package chuangyi.com.org.DOMIHome.Utils;

import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableArrayList;
import android.databinding.ViewDataBinding;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.List;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.Fragments.CommonFragment;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;
import chuangyi.com.org.ViewModel.CheckedMenuItem;
import chuangyi.com.org.ViewModel.ClickMenuItem;
import chuangyi.com.org.ViewModel.MenuItem;
import chuangyi.com.org.ViewModel.VideoListModel;
import me.maxwin.view.XListView;

/**
 * Created by zxh on 16/11/15.
 */
// expertcomplain 专家评论关注按钮状态
public class ListViewUtil {

    private static class ParagraphObserver extends DataSetObserver {
        private LinearLayout owner;
        private BaseAdapter adapter;

        public ParagraphObserver(LinearLayout owner, BaseAdapter adapter) {
            this.owner = owner;
            this.adapter = adapter;
            this.adapter.registerDataSetObserver(this);
            this.owner.setTag(this);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onChanged() {
            super.onChanged();
            updateViews();
        }

        @Override
        public void onInvalidated() {
            super.onInvalidated();
            updateViews();
        }

        private void updateViews() {
            owner.removeAllViews();
            for (int i = 0; i < adapter.getCount(); i++) {
                View view = adapter.getView(i, null, owner);
                owner.addView(view);
            }
        }

        public void unset() {
            adapter.unregisterDataSetObserver(this);
        }
    }

    @BindingAdapter("app:adapter")
    public static void listLoad(final AbsListView listView, BaseAdapter adapter) {
        listView.setAdapter(adapter);
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @BindingAdapter("app:xListAdapter")
    public static void XListViewLoad(final XListView listView, XListAdapterInterface adapter) {
        listView.setAdapter((BaseAdapter) adapter);
        listView.setPullLoadEnable(true);
        listView.setPullRefreshEnable(true);
        if (adapter != null) {
            adapter.setxListView(listView);
            listView.setXListViewListener(adapter.getxListViewListener());
            adapter.notifyDataSetChanged();
        }

        if (!listView.isPullLoading() && !listView.isPullRefreshing()) {
            listView.Refresh();
        } else {
            listView.stopLoadMore();
            listView.stopRefresh();
        }

    }

    @BindingAdapter("app:menuList")
    public static void menuLoad(final ViewGroup layout, List<CheckedMenuItem> menus) {
        layout.removeAllViews();
        if (menus != null) {
            Activity activity = (Activity) layout.getContext();
            LayoutInflater inflater = activity.getLayoutInflater();
            for (CheckedMenuItem menu : menus) {
                ViewDataBinding binding = DataBindingUtil.inflate(inflater, menu.getLayout(), layout, false);
                binding.setVariable(BR.menu, menu);
                layout.addView(binding.getRoot());
            }
        }
    }

    @BindingAdapter("app:paragraph")
    public static void loadParagraph(final LinearLayout linearLayout, ListBindingAdapter adapter) {
        if (adapter != null) {
            ParagraphObserver observer = (ParagraphObserver) linearLayout.getTag();
            if (observer != null) {
                observer.unset();
            }
            new ParagraphObserver(linearLayout, adapter);
        } else {
            linearLayout.removeAllViews();
        }
    }

    @BindingAdapter("app:paragraphs")
    public static void loadParagraph(final LinearLayout linearLayout, XListAdapterInterface adapter) {
        if (adapter != null) {
            ParagraphObserver observer = (ParagraphObserver) linearLayout.getTag();
            if (observer != null) {
                observer.unset();
            }
            new ParagraphObserver(linearLayout, (BaseAdapter) adapter);
        } else {
            linearLayout.removeAllViews();
        }
    }
}